package com.hisense.ovcloud.mqtt.warehouse.router;

import com.hisense.ovcloud.commons.configs.validator.BaseRouterConfiguration;
import com.hisense.ovcloud.commons.dto.resp.*;
import com.hisense.ovcloud.mqtt.warehouse.service.AuthService;
import com.hisense.ovcloud.mqtt.warehouse.service.DevicesService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;


@Configuration
public class WarehouseRoutersConfiguration extends BaseRouterConfiguration {

    private final DevicesService devicesService;

    private final AuthService authService;

    public WarehouseRoutersConfiguration(DevicesService devicesService, AuthService authService) {
        this.devicesService = devicesService;
        this.authService = authService;
    }

    @Bean
    public RouterFunction<ServerResponse> warehouseRoutes() {
        return RouterFunctions.nest(path("/zc-warehouse/v1"),
                route()
                        .POST("/getPublicKey", req -> ServerResponse.ok().body(devicesService.getPublicKey(req),
                                GetPublicKeyResp.class))
                        .POST("/registerDevices", req -> ServerResponse.ok().body(devicesService.registerDevices(req),
                                RegisterDevicesResp.class))
                        .POST("/registerProduct", req -> ServerResponse.ok().body(devicesService.registerProduct(req),
                                Boolean.class))
                        .POST("/createSqlTable", req -> ServerResponse.ok().body(devicesService.createSqlTable(req),
                                Boolean.class))
                        .POST("/generateAesKey", req -> ServerResponse.ok().body(devicesService.generateAesKey(req),
                                GenerateAesKeyResp.class))
                        .POST("/updateDeviceStatus", req -> ServerResponse.ok().body(devicesService.updateDeviceActiveStatus(req),
                                Boolean.class))
                        .POST("/generateAesKeyAndFetchPublicKey", req -> ServerResponse.ok().body(devicesService.generateAesKeyAndFetchPublicKey(req),
                                GenerateAesKeyAndFetchPublicKeyResp.class))
                        .POST("/authBatch", req -> ServerResponse.ok().body(devicesService.authBatch(req),
                                AuthBatchResp.class))
                        .build());
    }

    @Bean
    public RouterFunction<ServerResponse> authRoutes() {
        return RouterFunctions.nest(path("/"),
                route()
                        .POST("/mqtt/auth", authService::handleAuthClient)
                        .build());
    }
}